package com.xnyzc.lhy.order.controller.capi.user.pay;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.order.entity.param.order.OrderParam;
import com.xnyzc.lhy.order.entity.param.user.pay.UserPayParam;
import com.xnyzc.lhy.order.service.user.pay.IOaCUserPayService;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @Description 用户支付
 * @Author zhuyubin
 * @Date 2019/8/7 14:13
 **/
@RestController
@RequestMapping(value = "capi/oaCuserPay")
public class OaCUserPayController {

    @Autowired
    private IOaCUserPayService iOaCUserPayService;

    @GetMapping(value = "getUserPay")
    public Rv getUserPay(@ApiParam(name = "orderNo", value = "订单号") @RequestParam(value = "orderNo") String orderNo){
        Long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId)){
            throw PangException.create(EErrorCode.tokenExpire);
        }
        String tag = TokenUtil.getTag();
        if (StringUtils.isEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return Rv.wrap(iOaCUserPayService.getUserPay(userId, orderNo, tag));
    }

    @PostMapping(value = "userPay")
    public Rv userPay(@RequestBody UserPayParam userPayParam){
        Long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId)){
            throw PangException.create(EErrorCode.tokenExpire);
        }
        String tag = TokenUtil.getTag();
        if (StringUtils.isEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        String orderNo = userPayParam.getOrderNo();
        BigDecimal balancePay = userPayParam.getBalancePay();
        BigDecimal otherPay = userPayParam.getOtherPay();
        Integer payType = userPayParam.getPayType();
        if (StringUtils.isEmpty(orderNo) || CheckUtil.objIsEmpty(payType) || CheckUtil.objIsEmpty(balancePay) || CheckUtil.objIsEmpty(otherPay)){
            throw PangException.create(EErrorCode.argCheckErr);
        }
        return Rv.wrap(iOaCUserPayService.userPay(userId, tag, orderNo, balancePay, otherPay, payType));
    }

    /**
     * 获取支付信息【APP调用】
     *
     * @return
     * @author Mryang
     */
    @ResponseBody
    @PostMapping(value = "/getPayInfo")
    public Rv getPayInfo(@RequestBody UserPayParam userPayParam) {

        Long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId)){
            throw PangException.create(EErrorCode.tokenExpire);
        }
        String tag = TokenUtil.getTag();
        if (StringUtils.isEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        userPayParam.setUserId(userId);
        userPayParam.setTag(tag);
        return iOaCUserPayService.getPayInfo(userPayParam);
    }

}
